package cn.fsd.controller;

import cn.fsd.domain.Order;
import cn.fsd.domain.Transfer;
import cn.fsd.nacos.DeliveryServer;
import cn.fsd.nacos.DispatchServer;
import cn.fsd.query.delivery.PurchaseBillQuery;
import cn.fsd.query.dispatch.TransferQuery;
import cn.fsd.query.storeroom.TaskQuery;
import cn.fsd.service.StoreRoomService;
import cn.fsd.util.StoreroomRedisUtils;
import cn.fsd.vo.ResultBean;
import cn.fsd.vo.storeroom.transfer.TransferOutInfoVO;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/storeroom")
public class StoreRoomController {

    @Autowired
    StoreRoomService storeRoomService;

    @Autowired
    DeliveryServer deliveryServer;

    @Autowired
    DispatchServer dispatchServer;

    @Autowired
    StoreroomRedisUtils redisUtils;

    @RequestMapping(value = "/getInfo/{storeroom_id}", method = RequestMethod.GET)
    @ApiOperation("获取库房信息")
    public ResultBean getInfo(@PathVariable String storeroom_id) {
        return ResultBean.success(storeRoomService.getInfo(storeroom_id));
    }
    /**
     * 获取最大库存量,因为需要远程调用所以速度较慢
     */
    @RequestMapping(value = "/getMaxLimitation/{delivery_address}/{commodity_id}", method = RequestMethod.GET)
    @ApiOperation("获取最大库存量")
    public Integer getMaxLimitation(@PathVariable String delivery_address, @PathVariable String commodity_id) {
        return storeRoomService.getMaxLimitation(delivery_address, commodity_id);
    }

    /**
     * 订单创建之前用来查询缺货,因为需要远程调用所以速度较慢
     */
    @RequestMapping(value = "/checkStockOut/{delivery_address}/{commodity_id}/{commodity_num}",method = RequestMethod.GET)
    @ApiOperation("订单创建前，根据地址查询订单缺货")
    public Boolean checkStockOut(@PathVariable String delivery_address, @PathVariable String commodity_id, @PathVariable Integer commodity_num){
        return storeRoomService.checkStockOut(delivery_address,commodity_id,commodity_num);
    }

    /**
     * 检查订单是否缺货，通过中心库房id直接查询
     */
    @RequestMapping(value = "/checkStockOutById/{storeroom_id}/{commodity_id}/{commodity_num}",method = RequestMethod.GET)
    @ApiOperation("订单预创建后，查询订单缺货")
    public Boolean checkStockOutByStoreroomId(@PathVariable String storeroom_id,@PathVariable String commodity_id,@PathVariable Integer commodity_num){
        return storeRoomService.checkStockOutByStoreroomId(storeroom_id,commodity_id,commodity_num);
    }

    /**
     * 当订单缺货时，操作仓库-订单表，修改缺货数量并将订单加入等待队列
     */
    @RequestMapping(value = "/stockOutOP", method = RequestMethod.POST)
    @ApiOperation("订单缺货操作")
    ResultBean stockOutOP(@RequestBody Order order) {
        storeRoomService.stockOutOP(order);
        return ResultBean.success();
    }

    /**
     * 订单状态为缺货或可分配或已挂起时，退订该订单，从缺货量中删除，从队列中删除
     */
    @RequestMapping(value = "/cancelOrder", method = RequestMethod.POST)
    @ApiOperation("撤销订单操作")
    ResultBean cancelOrder(@RequestBody Order order) {
        storeRoomService.cancelOrder(order);
        return ResultBean.success();
    }


    @RequestMapping(value = "/handle",method = RequestMethod.POST)
    @ApiOperation("订单分配货物操作")
    ResultBean handle(@RequestBody Order order){
        storeRoomService.handle(order);
        return ResultBean.success();
    }

    /**
     * 查询某仓库未入库（purchase_status==0）的购货入库单（购货入库功能）
     * @param query String storeroom_id; Date start_date; Date end_date;
     */
    @RequestMapping(value = "/purchaseBill/find",method = RequestMethod.POST)
    @ApiOperation(value = "查询某仓库需要购货入库的信息")
    ResultBean findPurchaseBill(@RequestBody PurchaseBillQuery query){
        if(query.getStoreroom_id()==null||query.getStoreroom_id().equals("")){
            return ResultBean.error("storeroom_id必须指定");
        }
        return ResultBean.success(deliveryServer.getPurchaseBillByPage(query));
    }

    /**
     * 查询某仓库调拨单（中心库房调拨出库）
     *
     * (调拨单状态为”已调度“)，storeroom_id为本仓库id
     */
    @RequestMapping(value = "/transfer/find/out",method = RequestMethod.POST)
    @ApiOperation(value = "中心仓库调拨出库查询")
    ResultBean findTransferOut(@RequestBody TransferQuery query){
        if(query.getStoreroom_id()==null||query.getStoreroom_id().equals("")){
            return ResultBean.error();
        }
        return ResultBean.success(dispatchServer.findByStoreroomByPage(query));
    }

    /**
     * ,(调拨单状态为”已出库“)，station_id为本仓库id
     */
    @RequestMapping(value = "/transfer/find/in",method = RequestMethod.POST)
    @ApiOperation(value = "分站仓库调拨入库查询")
    ResultBean findTransferIn(@RequestBody TransferQuery query){
        if(query.getStation_id()==null||query.getStation_id().equals("")){
            return ResultBean.error();
        }
        return ResultBean.success(dispatchServer.findByStationByPage(query));
    }

    /**
     * (任务单状态为”已分配“)，station_id为本仓库id
     */
    @RequestMapping(value = "/transfer/getCommodity",method = RequestMethod.POST)
    @ApiOperation(value = "分站仓库领货查询")
    ResultBean findDeliveryOut(@RequestBody TaskQuery query){
        if(query.getStation_id()==null||query.getStation_id().equals("")){
            return ResultBean.error();
        }
        query.setTask_status("已分配");
        return ResultBean.success(storeRoomService.findDeliveryOut(query));
    }

    @GetMapping("/transfer/{transfer_id}")
    @ApiOperation(value = "查询某调拨单的详细信息")
    ResultBean getTransferById(@PathVariable String transfer_id){
        return ResultBean.success(storeRoomService.findTransferById(transfer_id));
    }

    @PostMapping("/transfer/changeStatus")
    @ApiOperation(value = "修改某调拨单的状态")
    ResultBean getTransferById(@RequestBody Transfer transfer){
        return ResultBean.success(dispatchServer.changeStatus(transfer));
    }

    /**
     * 传入指定日期，商品名（模糊查询），当前库房id
     *   返回当天当仓库根据某件商品统计的调拨出库总数
     *
     *   传入指定日期（必须），商品名（模糊查询），当前库房id（必须）
     */
    @PostMapping("/transfer/getTransferOutInfo")
    @ApiOperation(value = "出库单查询")
    ResultBean getTransferOutInfo(@RequestBody TransferQuery transferQuery){
        PageInfo<TransferOutInfoVO> transferOutInfo = storeRoomService.getTransferOutInfo(transferQuery);
        return ResultBean.success(transferOutInfo);
    }

    /**
     * 传入指定日期（必须），商品名（模糊查询），分站仓库名（模糊查询），当前库房id（必须）
     * 返回当天当仓库发往某仓库根据某件商品统计的调拨出库总数
     *
     * 传入指定日期（必须），商品名（模糊查询），分站仓库名（模糊查询），当前库房id（必须）
     */
    @PostMapping("/transfer/getDistributeInfo")
    @ApiOperation(value = "分发单查询")
    ResultBean getDistributeInfo(@RequestBody TransferQuery transferQuery){
        return ResultBean.success(storeRoomService.getDistributeInfo(transferQuery));
    }

    @PostMapping("/exchange/find/exchangeCheckIn")
    @ApiOperation(value = "分站退货登记查询")
    ResultBean findExchangeCheckIn(@RequestBody TaskQuery taskQuery){
        return ResultBean.success(storeRoomService.findExchangeCheckIn(taskQuery));
    }

    @PostMapping("/exchange/find/exchangeOut")
    @ApiOperation(value = "分站退货出库查询")
    ResultBean findExchangeOut(@RequestBody TaskQuery taskQuery){
        return ResultBean.success(storeRoomService.findExchangeOut(taskQuery));
    }

    @PostMapping("/exchange/find/exchangeIn")
    @ApiOperation(value = "中心库房退货入库查询")
    ResultBean findExchangeIn(@RequestBody TaskQuery taskQuery){
        return ResultBean.success(storeRoomService.findExchangeIn(taskQuery));
    }

    @PostMapping("/exchange/find/exchangeCenter")
    @ApiOperation(value = "中心站退货查询")
    ResultBean findExchangeCenter(@RequestBody TaskQuery taskQuery){
        return ResultBean.success(storeRoomService.findExchangeCenter(taskQuery));
    }

    @GetMapping("/beginExchange/{order_id}")
    @ApiOperation("换货订单开启退货流程")
    public ResultBean beginExchange(@PathVariable String order_id){
        storeRoomService.beginExchange(order_id);
        return ResultBean.success("开启退货流程");
    }

    @GetMapping("/clearRedis")
    @ApiOperation("清空缺货订单队列和撤销订单集合")
    public void beginExchange(){
        redisUtils.delDB();
    }


}
